package com.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.test.entity.User;
import com.test.mapper.UserMapperPlus;
import com.test.util.MyBatisPlusUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class UserMybatisPlusUtilTest
    // 原类中的代码（setup、insertUser、queryList、update方法）保持不变
 {
    private SqlSession sqlSession;
    private UserMapperPlus userMapperPlus;

    // 测试方法执行前初始化SqlSession和Mapper
    @Before
    public void setup() {
        sqlSession = MyBatisPlusUtil.getSqlSession();
        userMapperPlus = sqlSession.getMapper(UserMapperPlus.class);
        System.out.println("🚀 开始执行MyBatis CRUD测试...");
    }

    // 插入数据测试
    @Test
    public void insertUser() {
        User user = new User("张三123", "111@qq.com", 18);
        int num = userMapperPlus.insert(user);
        sqlSession.commit(); // 提交事务，确保数据写入数据库
        System.out.println("插入成功，条数是：" + num);
    }


    // 条件查询测试（查name=张三123，只查id/name，按id降序）
    @Test
    public void queryList() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name", "张三123")
                .select("id", "name")
                .orderByDesc("id");
        List<User> users = userMapperPlus.selectList(wrapper);
        users.forEach(user -> {
            System.out.println(user);
        });
    }

    // 更新数据测试（按id更新）
    @Test
    public void update() {
        User user = new User(12, "李四", "111@qq.com", 18);
        userMapperPlus.updateById(user);
        sqlSession.commit(); // 提交事务
    }
}